package com.luoyi.util;

import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.file.Files;
import java.util.Objects;

/**
 * 转换PDF
 *
 * @author xt
 */
@Slf4j
public class WordConvertPdfUtil {

    public static void main(String[] args) throws Exception {
        String sourceFilePath = "D:\\fileTest\\模板.xlsx";
        String targetFilePath = "D:\\fileTest\\1.pdf";
        excelConvertPdf(sourceFilePath, targetFilePath);
    }

    /**
     * Word转PDF操作
     *
     * @param sourceFile 源文件
     * @param targetFile 目标文件
     */
    public static void wordConvertPdf(String sourceFile, String targetFile) throws Exception {
        if (!getLicense("license.xml")) {
            log.info("验证License 失败");
            return;
        }
        FileOutputStream os = null;
        try {
            String path = getFilePath();
            long old = System.currentTimeMillis();
            //新建一个空白pdf文档
            File file = new File(targetFile);
            os = new FileOutputStream(file);
            // sourceFile是将要被转化的word文档
            Document doc = new Document(sourceFile);
            // start setting font
            FontSettings fontSettings = new FontSettings();
            // end
            doc.setFontSettings(fontSettings);
            fontSettings.setFontsFolder(path, true);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            os.close();
            long now = System.currentTimeMillis();
            log.info("转换PDF共耗时： {}", ((now - old)) + "毫秒");
        } catch (Exception e) {
            log.error("wordConvertPDF Exception异常: ", e);
            throw new Exception("wordConvertPDF Exception异常");
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                log.error("wordConvertPDF IOException异常:", e);
            }
        }
    }


    public static boolean getLicense(String file) {
        boolean result = false;
        InputStream is = null;
        try {
            String path = getFilePath() + File.separator + file;
            is = Files.newInputStream(new File(path).toPath());
            License asposeLic = new License();
            asposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            log.error("getLicense Exception异常: ", e);
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                log.error("getLicense IOException异常: ", e);
            }
        }
        return result;
    }


    public static String getFilePath() {
        String path = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(""))
                .getPath();
        return path + "jar" + File.separator + "aspose" + File.separator;
    }

    /**
     *
     */
    public static void excelConvertPdf(String sourcePath, String targetPath) {
        if (!getLicense("license.xml")) {
            return;
        }
        try {
            // 新建一个空白pdf文档
            File file = new File(targetPath);
            FileOutputStream os = new FileOutputStream(file);
            // 原始excel路径
            Workbook wb = new Workbook(sourcePath);
            FileOutputStream fileOS = new FileOutputStream(file);
            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
